﻿#ifndef BATCHTHREAD_H
#define BATCHTHREAD_H

/**
 喷嘴批量检测工作线程。这个线程中完成喷嘴批量检测的所有工作。
*/

#include <QtCore/QThread>
#include <QtCore/QList>
#include <QtCore/QVector>
#include <QtCore/QPoint>
#include <QtCore/QSemaphore>

#include "FocusMeasure.h"
class MainWindow;

class BatchThread : public QThread
{
    Q_OBJECT
public:
    BatchThread(QObject *parent = Q_NULLPTR);
    ~BatchThread();
    void setMainWindow(MainWindow * win);
    void addMeasurePoint(int row, int col, double x_mm, double y_mm, QString fileName);
    void resetMeasurePoints();
    void setScale(double scale) {m_scale = scale;}
    void setZPositions(QList<double> zList);

    /**
    * @brief 设置自动对焦和自动测量功能是否开启
    * @param f true 表示开启自动对焦
    * @param m true 表示开启自动测量
    * @return
    */
    void setFocusAndMeasure(bool f, bool m){m_autoFocus = f; m_autoMeasure = m;}
signals:
    void moveToPos(double x_mm, double y_mm, double speed);
    void moveToRelPos(double x_mm, double y_mm, double speed);
    void zMoveToPos(double z_mm, double speed);
    void batchMeasureFinish();
public slots:
    void stop();
    void xMoveFinished();
    void yMoveFinished();
    void zMoveFinished();

private:
    MainWindow *m_win;

    QList<int > m_cols;
    QList<int> m_rows;
    QList<double> m_posXmm;
    QList<double> m_posYmm;
    QStringList m_fileNames;

    QList<double> m_zList;

    QSemaphore m_xSemaphore;
    QSemaphore m_ySemaphore;
    QSemaphore m_zSemaphore;
    bool m_stop;
    bool m_autoFocus;
    bool m_autoMeasure;
    bool m_autoCenter;
    int m_lens;
    double m_scale;
    FocusMeasure m_focusMeasure;
    void initSemaphore();
    bool waitingForZStop();
    bool waitGotoPos();

    void autoFocus();
    void autoCenter();
    void autoMeasure();
protected:
    void run();
};

#endif // BATCHTHREAD_H
